﻿using System;
using Microsoft.FxCop.Sdk;

namespace Community.StaticAnalysis.Design.Rules
{
    /// <summary>
    /// The <see cref="ScopeDataMemberPropertiesAsPublic"/>
    /// class is used to identify properties marked with the DataMember attribute that are not public.
    /// </summary>
    internal sealed class ScopeDataMemberPropertiesAsPublic : CheckPropertyRule
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ScopeDataMemberPropertiesAsPublic"/> class.
        /// </summary>
        public ScopeDataMemberPropertiesAsPublic()
            : base(
                typeof (ScopeDataMemberPropertiesAsPublic).Name,
                typeof(ScopeDataMemberPropertiesAsPublic).Namespace + Constants.RulesResourceNameSuffix,
                typeof (ScopeDataMemberPropertiesAsPublic).Assembly)
        {
        }

        /// <summary>
        /// Checks the specified property.
        /// </summary>
        /// <param name="property">The property.</param>
        public override void Check(PropertyNode property)
        {
            // Check if this property is marked with DataMember
            if (RulesHelper.NodeHasAttribute(property, Constants.DataMemberFullName) == false)
            {
                // This is a property that isn't marked with DataMember
                return;
            }

            // Check if the property is public
            if (property.IsPublic == false)
            {
                // This data member property is not public
                Problems.Add(new Problem(GetResolution(new Object[] { property.FullName})));
            }
        }
    }
}